package vista;

import java.util.ArrayList;
import java.util.List;

import modelo.Empleado;
import modelo.Planificacion;

import org.joda.time.LocalDate;
import org.joda.time.LocalTime;
import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.aop.windows.TransactionalDialog;
import org.uqbar.arena.bindings.NotNullObservable;
import org.uqbar.arena.bindings.ObservableProperty;
import org.uqbar.arena.bindings.PropertyAdapter;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.Selector;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.Dialog;
import org.uqbar.arena.windows.WindowOwner;
import org.uqbar.commons.utils.FireChange;
import org.uqbar.commons.utils.Observable;
import org.uqbar.lacar.ui.model.Action;
import org.uqbar.lacar.ui.model.ListBuilder;
import org.uqbar.lacar.ui.model.bindings.Binding;

import com.uqbar.commons.collections.Transformer;

import applicationModels.AsignadorDeEmpleados;
import applicationModels.IntObservable;


public class AgregarAsignacionWindow extends TransactionalDialog<AsignadorDeEmpleados> {


	public AgregarAsignacionWindow(CrearPlanificacionWindow parent,
			Planificacion planificacion) {
		super(parent, new AsignadorDeEmpleados(planificacion));
	}

	@Override
	protected void createMainTemplate(Panel mainPanel) {
		this.setTitle("Crear asignación");
		this.setTaskDescription("Agregar una asignacion para el " + this.getModelObject().getFecha());
		
		super.createMainTemplate(mainPanel);
		
		this.createResultsGrid(mainPanel);
	}
	
	@Override
	protected void createFormPanel(Panel mainPanel) {
		Panel form = new Panel(mainPanel);
		form.setLayout(new ColumnLayout(3));
		
		Selector<IntObservable> selector = new Selector<IntObservable>(form) //
				.allowNull(true);
		selector.bindValueToProperty("inicio");
		selector.setWidth(24);
		
		
		Binding<ListBuilder<IntObservable>> itemsBinding = selector.bindItems( //
				new ObservableProperty(this.getModelObject(), "horariosPosiblesSelector1"));
				
		
		Integer n = new Integer(3);
		
		
		Selector<IntObservable> selector2 = new Selector<IntObservable>(form) //
				.allowNull(true);
		selector2.bindValueToProperty("fin");
		selector2.setWidth(24);
		
		Binding<ListBuilder<IntObservable>> itemsBinding2 = selector2.bindItems( //
				new ObservableProperty(this.getModel(), "horariosPosiblesSelector2"));
		
		//selector.onSelection(new MessageSend(this.getModelObject(), "calcularInts"));
		
		
		Button buttonBuscar = new Button(mainPanel);
		
		buttonBuscar
		.setCaption("Buscar")
		.onClick(new MessageSend(this.getModelObject(), "buscar"))
		.setAsDefault()
		.disableOnError();
		
		NotNullObservable elementSelected2 = new NotNullObservable("fin");
		buttonBuscar.bindEnabled(elementSelected2);
	}

	
	
	@Override
	protected void addActions(Panel actionsPanel) {
		Button buttonAceptar = new Button(actionsPanel);
		buttonAceptar
		.setCaption("Asignar")
		.onClick(new MessageSend(this, "accept"))
		.setAsDefault()
		.disableOnError();
		
		NotNullObservable elementSelected = new NotNullObservable("empleadoSeleccionado");
		buttonAceptar.bindEnabled(elementSelected);
		
	}
	
	@Override
	public void accept() {
		this.getModelObject().guardarAsignacion();
		super.accept();
	}
	
	
	protected void createResultsGrid(Panel mainPanel) {
		Table<Empleado> table = new Table<Empleado>(mainPanel, Empleado.class);
		table.setHeigth(200);
		table.setWidth(450);

		table.bindItemsToProperty("empleados");
		table.bindValueToProperty("empleadoSeleccionado");

		this.describeResultsGrid(table);

	}
	
    private void describeResultsGrid(Table<Empleado> table) {
		
		new Column<Empleado>(table) //
		.setTitle("Nombre")
		.setFixedSize(150)
		.bindContentsToProperty("nombre");

	    new Column<Empleado>(table) //
		.setTitle("Disponibilidad")
		.setFixedSize(450)
		.bindContentsToTransformer(new Transformer<Empleado, String>(
				) {

					@Override
					public String transform(Empleado e) {
						return getModelObject().getDisponibilidad(e);
					}
		});

	}
    
    
	

	
}
